﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WisDom.UnifiedPortal.Models;
using WisDom.UnifiedPortal.Services;
using WisDom.Services;
using WisDom.Models;
using WisDom.DBContext;
using WisDom.IService;
using System.Text;

namespace WisDom.UnifiedPortal
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            string dbConnectionString = Configuration.GetConnectionString("DefaultConnection");

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            services.Configure<DbContextOption>(option =>
            {
                option.ConnectionString = dbConnectionString;
                option.DbType = DbType.MSSQLSERVER;
            });

            services.Configure<IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });


            AddDependencies(services);

            services.AddDbContext<ApplicationDbContext>();

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            // Add application services.
            services.AddTransient<IEmailSender, EmailSender>();
            services.AddMemoryCache();

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles(); 

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                  name: "admin",
                   template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );
            });
          
        }
        /// <summary>
        /// 通过依赖注入service
        /// </summary>
        public IServiceCollection AddDependencies(IServiceCollection services)
        {
            services.AddScoped<IUPDataSourceInterface,UPDataSourceService>();
   
            return services;
        }

    }
}
